<?php

namespace App\Models\Api;

use Carbon\Carbon;

class Interview extends ApiModel
{
    //表名
    protected $table = 'interview';

    protected $guarded = [];

    /**
     * The attributes that should be hidden for arrays.
     * 隐藏字段
     * @var array
     */
    protected $hidden = ['company_uid', 'com_id', 'personal_uid',
        'created_at', 'updated_at', 'resume', 'resume_id',
        'company_job', 'company_basic'];

    public function company_basic()
    {
        return $this->hasOne(CompanyBasic::class, 'id', 'com_id')
            ->select('id', 'company_name', 'contacts', 'mobile', 'phone', 'address', 'lines');
    }

    //公司名
    public function getCompanyNameAttribute()
    {
        return $this->company_basic ? $this->company_basic->company_name : '';
    }

    //公司地址
    public function getAddressAttribute()
    {
        return $this->company_basic ? $this->company_basic->address : '';
    }

    //公司路线
    public function getLinesAttribute()
    {
        return $this->company_basic ? $this->company_basic->lines : '';
    }

    public function company_job()
    {
        return $this->hasOne(CompanyJob::class, 'id', 'job_id')
            ->select('id', 'job_title', 'contact_info', 'salary_above', 'salary_below');
    }

    //职位名
    public function getJobTitleAttribute()
    {
        return $this->company_job ? $this->company_job->job_title : '';
    }

    //薪资
    public function getSalaryAttribute()
    {
        return $this->company_job ? $this->company_job->salary : '';
    }

    //更新时间
    public function getTimeAttribute()
    {
        return $this->updated_at->format('Y-m-d');
    }

    public function resume()
    {
        return $this->hasOne(Resume::class, 'id', 'resume_id')
            //完全保密不给查看
            ->where('bkresume_status', '!=', 3)
            ->with(['homeaddress_area_name'])
            ->select('id', 'name', 'sex', 'birthday', 'education', 'updatetime',
                'intentionjobs', 'avatar', 'homeaddress', 'chkphoto_open');
    }

    //头像
    public function getAvatarAttribute()
    {
        if (!$this->resume || $this->resume->chkphoto_open == 1) {//隐藏头像
            return '';
        }
        return $this->resume->avatar;
    }

    //简历名
    public function getNameAttribute()
    {
        return $this->resume ? $this->resume->name : '';
    }

    //性别
    public function getSexAttribute()
    {
        return $this->resume ? $this->resume->sex : '';
    }

    //生日
    public function getBirthdayAttribute()
    {
        return $this->resume ? $this->resume->birthday : '';
    }

    //年龄
    public function getAgeAttribute()
    {
        return $this->resume ? $this->resume->age : '';
    }

    //期望工作
    public function getIntentionjobsAttribute()
    {
        return $this->resume ? $this->resume->intentionjobs : '';
    }

    //学历
    public function getEducationNameAttribute()
    {
        return $this->resume ? $this->resume->education_name : '';
    }

    //现住址
    public function getHomeaddressNameAttribute()
    {
        return $this->resume ? $this->resume->homeaddress_name : '';
    }

    public function getInterviewTimeAttribute($value)
    {
        return Carbon::parse($value)->format('Y-m-d H:i');
    }

}
